// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    // const logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)
    this.updateManager()
    // this.onShareAppMessage()

    // 登录
    wx.login({
      success: res => {
        console.log(res)
        wx.request({
          url: this.globalData.Interfaceurl+'getXcxOpenId',
          method:'post',
          header:{
            "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
          },
          data: {
            code: res.code,
          },
          success:res => {
            console.log(res)
            this.globalData.userId=res.data.user.id
            if (res.data.user.wxHeadImage!='') {
              wx.setStorageSync('isUserInfo', 0)
              // this.globalData.userinfoimg=res.data.user.wxHeadImage
            }else{
              wx.setStorageSync('userInfo', '')
            }
            if (res.data.user.mobile=='') {
              wx.setStorageSync('mobile', '')
              // this.globalData.userinfoimg=res.data.user.wxHeadImage
            }else{
              // wx.setStorageSync('mobile', res.data.user.mobile)
            }
           
          }
          

        })
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })


    wx.loadFontFace({
      family: 'SourceHanSansCN',
      source: 'url("https://at1984.oss-cn-shenzhen.aliyuncs.com/luckcow/fonts/SourceHanSansCN-Regular.otf")',
      global: true,
      success: function (e) {
          console.log(e, '动态加载字体成功')
      },
      fail: function (e) {
          console.log(e, '动态加载字体失败')
      }
  })

   
  },


  onShareAppMessage(){
        wx.onAppRoute(() =>{
          console.log('当前页面路由发生变化 触发该事件onShareAppMessage')
          const pages = getCurrentPages() //获取加载的页面
          const view = pages[pages.length - 1] //获取当前页面的对象
          if(!view)  return false  //如果不存在页面对象 则返回

    console.log(view.data)
    console.log(view.data.shopdetails)
         if(view.route=="pages/details/details"){
           
          view.onShareAppMessage = () => { //重写分享配置
                      return {
                        title: view.data.shopdetails.goodsName,
                        path: '/'+view.route+'?goodsid='+view.options.goodsid, //若无path 默认跳转分享页
                        imageUrl:view.data.imageUrl+view.data.shopdetails.goodsBannerList[0] //若无imageUrl 截图当前页面
                      }
                    }

         }else{
          view.onShareAppMessage = () => { //重写分享配置
                      return {
                        title: '好运牛',
                        path: '/'+view.route, //若无path 默认跳转分享页
                        imageUrl:'../../images/logoshare.png' //若无imageUrl 截图当前页面
                      }
                    }
         }
          // 若想给个别页面做特殊处理 可以给特殊页面加isOverShare为true 就不会重写了
          // const data = view.data
          // if (!data.isOverShare) {
            // data.isOverShare = true
            
          // }
        })
      },


 compareVersion(v1, v2) {
   console.log("v1",v1)
   console.log("v2",v2)
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)
  
    while (v1.length < len) {
      v1.push('0')
    }
    while (v2.length < len) {
      v2.push('0')
    }
  
    for (let i = 0; i < len; i++) {
      const num1 = parseInt(v1[i])
      const num2 = parseInt(v2[i])
  
      if (num1 > num2) {
        console.log('1')
        return 1
      } else if (num1 < num2) {
        console.log('2')
        return -1
        
      }
    }
  },



  wxlogin() {
    // wx.clearStorageSync()
    return new Promise((resolve, reject) => {
      wx.login({
        success: res => {
          // this.code = res.code;
          // let url = `你的请求地址`
          // wx.request({
          //   url: url, //开发者服务器接口地址",
          //   data: 'data', //请求的参数",
          //   method: 'GET',
          //   dataType: 'json', //如果设为json，会尝试对返回的数据做一次 JSON.parse
          //   success: res => {
          //    //处理逻辑
          //     resolve(res.data);//返回数据
          //   },
          //   complete: () => {
          //   }
          // });


          wx.request({
            url: this.globalData.Interfaceurl+'getXcxOpenId',
            method:'post',
            header:{
              "content-type": "application/x-www-form-urlencoded;charset=UTF-8"
            },
            data: {
              code: res.code,
            },
            success:res => {
              console.log(res)
              resolve(res.data);
            }
            
  
          })


          
        },
        fail: (err) => { 
          console.log(err)
        },
        complete: () => { }
      });
    })
  },

    // 检测新版本
updateManager() {
  const updateManager = wx.getUpdateManager()
  updateManager.onUpdateReady(function () {
  wx.showModal({
  title: '更新提示',
  content: '新版本已经准备好，是否重启应用？',
  success(res) {
  if (res.confirm) {
  updateManager.applyUpdate()
  }
  }
  })
  })
  },
  

//   测试地址：https://test.at1984.com/luckcow/a/api/yeahc
// 正式地址：https://hyn.at1984.com/luckcow/a/api/yeahc
  globalData: {
    userId:'',
    userInfo: null,
    Interfaceurl:'https://hyn.at1984.com/luckcow/a/api/yeahc/',
    Imageurl:'https://hyn.at1984.com',
  }
})
